<template>
<view>
<view class="container">
  <view class="user_top">
    <block v-if="loginUser">
      <image class="user_head_img" @tap="loginOut" :src="loginUser.platformUser.headimgurl + '?x-oss-process=style/preview_120'"></image>
      <text class="user_title">用户名称</text>
      <text class="user_name" @tap="loginOut">{{loginUser.platformUser.nickname}}</text>
    </block>
    <block v-else>
      <image class="user_head_img" @tap="login" src></image>
      <text class="user_title">用户名称</text>
      <text class="user_name" @tap="login">未登入</text>
    </block>
    <view class="order_menu">
      <text class="order_menu_item" v-for="(cells, index) in orderData.cells" :key="index" :data-link="cells.linkUrl" @tap="tolinkUrl">{{cells.text}}</text>
    </view>
    <view class="userShareEwm" @tap="showPoster" style="box-shadow:6rpx 6rpx 8rpx #55555533">
      <image class="userEwmCode" src="https://image1.sansancloud.com/xianhua/2019_8/20/9/42/26_903.jpg?x-oss-process=style/preview_120"></image>
    </view>
    <view class="userBindWxGz" @tap="clickWxGz">
      <!-- <web-view src="https://www.baidu.com/"></web-view> -->
      <image class="userBindWxGzCode" v-if="!loginUser.platformUser.openid" src="https://image1.sansancloud.com/naifen/2019_10/29/14/21/4_730.jpg?x-oss-process=style/preview_120"></image>
      <image class="userBindWxGzCode" v-else src="https://image1.sansancloud.com/naifen/2019_10/29/14/23/43_119.jpg?x-oss-process=style/preview_120"></image>
    </view>
  </view>
  <!-- 海报 -->
  <posters-user-info-component :ewmImgUrl="ewmImgUrl" v-if="posterState" @closePoaster="getChilrenPoster"></posters-user-info-component>
  <view class="user_menu">
    <view class="user_menu_item" v-for="(cells, index) in ListData.cells" :key="index" :data-link="cells.linkUrl" @tap="tolinkUrl">
      <view class="user_menu_item_link" v-if="!cells.id">
        <view class="user_menu_item_left">
          <image class="user_menu_item_icon" :src="cells.iconPath"></image>
          <text class="user_menu_item_name">{{cells.text}}</text>
        </view>
        <image class="user_menu_item_more" src="https://image1.sansancloud.com/xianhua/2019_3/27/15/44/8_298.jpg?x-oss-process=style/preview_120"></image>
      </view>
      <button open-type="contact" plain="true" hover-class="none" style="border:none" class="user_menu_item_link kefu" v-if="cells.id==1">
        <view class="user_menu_item_left">
          <image class="user_menu_item_icon" :src="cells.iconPath"></image>
          <text class="user_menu_item_name">{{cells.text}}</text>
        </view>
        <image class="user_menu_item_more" src="https://image1.sansancloud.com/xianhua/2019_3/27/15/44/8_298.jpg?x-oss-process=style/preview_120"></image>
      </button>
    </view>
  </view>
  <view class="user_footer">
    <image class="user_footer_img" mode="aspectFit" src="https://image1.sansancloud.com/xianhua/2019_3/27/15/46/41_583.jpg"></image>
  </view>
</view>
<!-- 授权窗口 -->
  <view v-if="showPopup">
  <!-- authorizationCount==1&& -->
    <view class="authorizationBg"></view>
    <view class="authorization">
      <view class="authorizationPopup">
        <view class="tipContent">
          <text style="font-size:38rpx;">前往微信授权</text>
        </view>
        <view class="buttonArray">
          <button class="button cancel" @tap="cancel">取消</button>
          <button class="button confirm" open-type="getUserInfo" :style="'color:' + setting.platformSetting.defaultColor" @getuserinfo="bindGetUserInfo">确定</button>
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>
// var tab = require("../../view/js/tab.js");
// var detailList = require("../../view/js/detail_list.js");

// import componentTagCustompage from "../../view/partials/custom_page/index";

export default {
  data() {
    return {
      /* seeting */
      setting: null,
      userData: null,
      PaiXuPartials: null,
      loginUser: null,
      componentState: true,
      //组件的data
      orderData: null,
      ListData: null,
      showPopup: false,
      // headData:{
      //   // imageUrl:'http://image.aikucun.xyz/aikucun/2018_4/2/11/48/3_10.jpg'
      // },
      blankData: {
        color: "rgb(244,244,244)",
        height: 12
      },
      orderData: {
        cells: [{
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/47/6_417.jpg",
          linkUrl: "order_list_2.html?easyStatus=2&easyStatusName=待付款",
          text: "待付款",
          color: "#777777",
          showCountNum: 0
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/45/21_775.jpg",
          linkUrl: "order_list_0.html?easyStatusName=全部订单",
          text: "全部订单",
          color: "#777777",
          showCountNum: 0
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/45/21_802.jpg",
          linkUrl: "order_list_3.html?easyStatus=3&easyStatusName=待发货",
          text: "待发货",
          color: "#777777",
          showCountNum: 0
        }],
        column: 3,
        showType: 0
      },
      ListData: {
        cells: [{
          iconPath: "https://image1.sansancloud.com/xianhua/2019_3/27/15/37/19_117.jpg?x-oss-process=style/preview_120",
          linkUrl: "address.html",
          text: "收货地址",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2019_3/27/15/37/19_432.jpg?x-oss-process=style/preview_120",
          linkUrl: "my_favorite.html",
          text: "我的收藏",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2019_3/27/15/37/19_434.jpg?x-oss-process=style/preview_120",
          linkUrl: "my_coupons.html",
          text: "优惠券",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2019_3/27/15/37/19_429.jpg?x-oss-process=style/preview_120",
          linkUrl: "",
          id: "1",
          text: "在线客服",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2019_3/27/15/37/19_420.jpg?x-oss-process=style/preview_120",
          linkUrl: "custom_page_my_newlist?pageNage=常见问题",
          text: "常见问题",
          color: "#777777"
        }],
        column: 3,
        showType: 0
      },
      posterState: false,
      ewmImgUrl: "",
      userInfo: ""
    };
  },
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
	//#ifdef MP
    if(getApp().globalData.loginUser){
        wx.getSetting({
          //检查用户是否授权了
          success(res) {
            console.warn("======检查用户是否授权了========", res);
        
            if (!res.authSetting['scope.userInfo']) {
              console.log('=====没授权===='); // that.showPopup = true
        
              that.setData({
                showPopup: true
              });
            } else {
              console.log('=====已授权====');
              that.dellSData();
              that.getSessionUserInfo();
              that.getParac();
            }
          }
        
        });
    }else{
        that.dellSData();
        that.getSessionUserInfo();
        that.getParac();
    }
	//#endif
	//#ifdef APP-PLUS
		that.dellSData();
		that.getSessionUserInfo();
		that.getParac();
	//#endif
    that.setData({
      loginUser: getApp().globalData.loginUser,
      userInfo: getApp().globalData.userInfo,
      setting: getApp().globalData.setting
    });
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: "#10c2ff"
    });
    wx.setNavigationBarTitle({
      title: "奶爸无忧·母婴商超"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  onShow: function () {
    let that = this;

    // if (this.openShow) {
    //   wx.getSetting({
    //     //检查用户是否授权了
    //     success(res) {
    //       console.warn("======检查用户是否授权了========", res);

    //       if (!res.authSetting['scope.userInfo']) {
    //         console.log('=====没授权===='); // that.showPopup = true

    //         that.setData({
    //           showPopup: true
    //         });
    //       } else {
    //         console.log('=====已授权====');
    //       }
    //     }

    //   });
    //   this.setData({
    //     loginUser: getApp().globalData.loginUser
    //   });
    //   this.getSessionUserInfo();
    // }

    // this.openShow = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getSessionUserInfo();
    this.getParac();
    console.log(this.setting);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */

  /* 分享 */
  onShareAppMessage: function () {
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  methods: {
    clickWxGz: function () {
      let that = this;
      console.log('===bindWxGz====', that.loginUser);
      let loginUser = that.loginUser;
      let title = "你确定要绑定公众号推送嘛~";

      if (loginUser.platformUser.openid) {
        title = "你确定要解绑公众号推送嘛~";
      }

      wx.showModal({
        title: '提示',
        content: title,
        success: function (res) {
          if (res.confirm) {
            if (!loginUser.platformUser.openid) {
              that.bindWxGz();
            } else {
              that.unBindWxGz();
            }
          } else if (res.cancel) {}
        }
      });
    },
    bindWxGz: function () {
      let that = this;
      let paramsUrl = "https://mini.sansancloud.com/chainalliance/" + getApp().globalData.clientNo + "/bindWxGz.html?platformUserId=" + that.loginUser.platformUser.id;
      that.tolinkUrl(paramsUrl);
      that.getSessionUserInfo();
    },
    unBindWxGz: function () {
      let that = this;
      let customIndex = getApp().globalData.AddClientUrl("/wx_unbind_wx_gz_openid.html"); //拿custom_page

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log("====== res.data=========", res.data);
          wx.hideLoading();

          if (!res.data.errcode || res.data.errcode == '0') {
            wx.showToast({
              title: '解绑成功~',
              icon: 'success',
              duration: 1000
            });
            that.getSessionUserInfo();
          } else {
            console.log('加载失败');
            wx.showToast({
              title: res.data.errMsg + '~',
              image: '/wxcomponents/images/icons/tip.png',
              duration: 1000
            });
          }
        },
        fail: function (res) {
          console.log(res);
          wx.hideLoading();
        }
      });
    },

    // 关闭海报
    getChilrenPoster(e) {
      let that = this;
      that.setData({
        posterState: false
      });
    },

    showPoster: function () {
      let that = this;
      console.log('===showPoster====', that.loginUser.id);

      if (that.loginUser && that.loginUser.platformUser.id) {
        let ewmImgUrl = getApp().globalData.getQrCode({
          type: "user_info",
          id: that.loginUser.platformUser.id
        });
        that.setData({
          posterState: true,
          ewmImgUrl: ewmImgUrl
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '您还未登录，点击【确定】重新加载',
          success: function (res) {
            if (res.confirm) {
              that.getSessionUserInfo();
              this.getParac();
            } else if (res.cancel) {}
          }
        });
      }
    },
    getParac: function () {
      var that = this;
      var customIndex = getApp().globalData.AddClientUrl("/custom_page_userinfo.html", {}, 'get', '1'); //拿custom_page

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log("====== res.data=========", res.data);
          wx.hideLoading();

          if (!res.data.errcode || res.data.errcode == '0') {
            that.setData({
              componentState: true
            });
          } else {
            console.log('加载失败');
            that.setData({
              componentState: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '加载失败，点击【确定】重新加载',
            success: function (res) {
              if (res.confirm) {
                that.getParac();
              } else if (res.cancel) {
                getApp().globalData.toIndex();
              }
            }
          });
        }
      });
    },
    loginOut: function () {
      wx.navigateTo({
        url: '/pages/pre_change_user_info/index'
      });
    },
    login: function () {
      wx.navigateTo({
        // url: '../login_wx/index',
        url: '/pages/login_wx/index'
      });
    },
    dellSData: function () {
      this.setData({
        orderData: this.orderData,
        ListData: this.ListData
      });
    },
    getSessionUserInfo: function () {
      var that = this;
      var postParamUserBank = getApp().globalData.AddClientUrl("/get_session_userinfo.html");
      wx.request({
        url: postParamUserBank.url,
        data: postParamUserBank.params,
        header: getApp().globalData.headerPost,
        success: function (res) {
          console.log(res.data);

          if (res.data.errcode == '0') {
            let UserInfo = res.data.relateObj.platformUser;
            let orderData = that.orderData;
            orderData.cells[0].showCountNum = UserInfo.unpayedCount;
            orderData.cells[1].showCountNum = UserInfo.unsendedCount;
            orderData.cells[2].showCountNum = UserInfo.unreceivedCount;
            that.setData({
              orderData: orderData,
              loginUser: res.data.relateObj
            });
            getApp().globalData.loginUser = res.data.relateObj;
          } else {
            wx.showToast({
              title: res.data.errMsg,
              image: '/wxcomponents/images/icons/tip.png',
              duration: 1000
            });
          }
        },
        fail: function (res) {
          // unsendedCount //待发货
          // unreceivedCount //待收货
          // unpayedCount //逮住款
          console.log(res.data);
        },
        complete: function (res) {
          wx.stopPullDownRefresh();
        }
      });
    },
    bindGetUserInfo: function (e) {
      let that = this;
      that.setData({
        showPopup: false
      });
      console.log(that.showPopup);
      console.log(e.detail.userInfo);

      if (e.detail.userInfo) {
        //用户按了允许授权按钮
        console.log('用户按了允许授权按钮');
        that.dellSData();
        that.getSessionUserInfo();
        that.getParac();

        if (getApp().globalData.loginUser && getApp().globalData.loginUser.platformUser && !getApp().globalData.loginUser.platformUser.nickname) {
          getApp().globalData.sentWxUserInfo(getApp().globalData.loginUser);
        }
      } else {
        console.log('用户按了拒绝按钮'); //用户按了拒绝按钮
      }
    },
    cancel: function () {
      this.setData({
        showPopup: false
      });
    },

    /* 组件事件集合 */
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget ? e.currentTarget.dataset.link : e;
      getApp().globalData.linkEvent(linkUrl);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>